library(foreign)
library(dplyr)
library(lattice)
library(latticeExtra)
library(grid)
library(gridExtra)
library(ggplot2)
library(ggthemes)
library(ggpubr)




# Figure 1
d <- read.csv("perceived diversity.csv")

ggplot(d, aes(x = who, y = est, ymin = lb, ymax = ub)) +
  geom_pointrange()+
  coord_flip() +
  theme_few() +
  theme(aspect.ratio = 1) +
  scale_x_discrete("Agreement with 'elections will probabily\nlead to more [of these types]\nof judges on courts'") +
  scale_y_continuous("Mean Response (1-5)")



# Figure 2
d3 <- read.dta("saved_old.dta")


plot1 <- (ggplot(d3, aes(y = curbElect2, x = sexism2)) +
            geom_point(color = "darkgray", shape = 1) +
            geom_jitter(color = "darkgray", shape = 1) +
            geom_smooth(se = FALSE, method = loess, color = "black") +
            theme_few() +
            theme(aspect.ratio = 1) +
            scale_x_continuous("Political Sexism") +
            scale_y_continuous("Support for\nJudicial Elections")
)

plot2 <- (ggplot(d3, aes(y=curbElect2, x = efficacy2)) +
            geom_point(color = "darkgray", shape = 1) +
            geom_jitter(color = "darkgray", shape = 1) +
            geom_smooth(se = FALSE, method = loess, color = "black") +
            theme_few() +
            theme(aspect.ratio = 1) +
            scale_x_continuous("Perceived Representation") +
            scale_y_continuous("Support for\nJudicial Elections")
)

plot3 <- (ggplot(d3, aes(y=curbElect2, x = racial2)) +
            geom_point(color = "darkgray", shape = 1) +
            geom_jitter(color = "darkgray", shape = 1) +
            geom_smooth(se = FALSE, method = loess, color = "black") +
            theme_few() +
            theme(aspect.ratio = 1) +
            scale_x_continuous("Racial Attitudes") +
            scale_y_continuous("Support for\nJudicial Elections")
)


ggarrange(plot1, plot2, plot3,
          labels = c("A", "B", "C"),
          ncol = 2, nrow = 2,
          align = "v")




# Figure 3
d0 <- read.csv("ols_estimates.csv")

d0$var <- reorder(d0$var, d0$est)

ggplot(d0, aes(x = var, y = est, ymin = lb, ymax = ub)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
  geom_pointrange() +
  coord_flip() +
  theme_few() +
  theme(aspect.ratio = 1) +
  scale_x_discrete("") +
  scale_y_continuous("OLS Coefficient") 



# Figure 4
d5 <- read.csv("effects.csv")

ggplot(d5, aes(x = x, y = est)) +
  geom_line() +
  geom_line(aes(x=x, y = lb), linetype = "dashed") +
  geom_line(aes(x=x, y = ub), linetype = "dashed") +
  facet_wrap(~var2, labeller = labeller(var2 = c(`1` = "Perceived Representation", `2` = "Political Sexism", `3` = "Implicit Racial Attitudes"))) +
  theme_bw() +
  theme(aspect.ratio = 1,
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  scale_x_continuous("") +
  scale_y_continuous("Predicted Support for\nJudicial Elections")



# Figure 5

d <- read.csv("interactions.csv")
ggplot(d, aes(x = x, y = est, ymin = lb, ymax = ub)) +
  geom_hline(yintercept = 0, color = "gray", linetype = "dashed") +
  geom_pointrange() +
  facet_wrap(~var2,labeller = labeller(var2 = c(`1` = "Perceived Representation", `2` = "Political Sexism", `3` = "Implicit Racial Attitudes"))) +
  theme_bw() +
  theme(aspect.ratio = 1,
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) + 
  scale_x_continuous("Political Sophistication") +
  scale_y_continuous("Effect on Support\nfor Judicial Elections")






